package com.nit.bike.controller.bike;

import com.nit.bike.controller.basic.StationController;
import com.nit.bike.dto.basic.BikeDTO;
import com.nit.bike.dto.bike.BikeRecordDTO;
import com.nit.bike.dto.system.TokenDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.service.basic.BikeService;
import com.nit.bike.service.bike.BikeRecordService;
import com.nit.bike.utils.AuthorizationUtil;
import com.nit.bike.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/api/bikeRecord")
public class BikeRecordController {
    @Resource
    private BikeRecordService bikeRecordService;
    private final Logger logger = LoggerFactory.getLogger(BikeRecordController.class);

    /**
     * 获取数据列表
     */
    @RequestMapping(value = "/listBikeRecord", method = RequestMethod.POST)
    public TableRspDTO listByStation(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO req) {
        TableRspDTO rsp = null;
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (AuthorizationUtil.userInterfaceBool(tokenDTO)) {
                rsp = bikeRecordService.list4Table(req);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * @Description: 新增记录
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody BikeRecordDTO bikeRecordDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (AuthorizationUtil.userInterfaceBool(tokenDTO)) {
                op.setMessage(bikeRecordService.add(bikeRecordDTO).toString());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
}
